<template>
<div id="login">
         <!-- 注册页面的整体盒子 -->

    <!-- 注册的盒子 -->
    <div class="register-box">
      <!-- 标题的盒子 -->
    <div class="title-box"></div>
      <!-- 注册的表单区域 -->
    <el-form class="login-box" :model="userList" :rules="userListRules" ref="loginForm">
        <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账户" v-model="userList.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" show-password v-model="userList.password"></el-input>
        </el-form-item>
          <el-button type="primary" class="login" @click="loginBig">登陆</el-button>
          <el-button type="text" class="register"><a href="/register">去注册</a></el-button>
    </el-form>

    </div>
</div>

</template>

<script>
import { lgoAPI } from '@/api/login'
export default {
  data () {
    return {
      userList: {
        username: '',
        password: ''
      },
      userListRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            pattern: /^[a-z\d]{1,10}$/i,
            message: '请输入正确格式的用户名',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,15}$/,
            message: '请输入正确格式的密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    loginBig () {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return
        const {
          data: { code, message, token }
        } = await lgoAPI(this.userList)
        if (code !== 0) return this.$message.success(message)
        this.$message.success(message)
        console.log(token)
        this.$store.commit('user/setToken', token)
        this.$router.replace('/')
      })
    }
  }
}
</script>

<style lang="less" scoped>
    .el-form {
        margin: 0 20px;
        padding: 50px 10px;
        border-top: 1px solid #ccc;
    }
    .login {
        width: 100%;

    }
    .register {
        margin-top: 43px;
    }
  .register-box {
    width: 400px;
    height: 405px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 40px 15px 0;
}
    .title-box {
      margin: 20px;
      height: 60px;
      background: url('~@/assets/images/login_title.png') center no-repeat;
    }

</style>
